package cn.edu.zafu.bigdata.zengyadian;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class StudentInformationManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();
            try (Connection connection = DBUtil.getConnection()) {
                switch (choice) {
                    case 1:
                        addStudent(scanner, connection);
                        break;
                    case 2:
                        searchStudent(scanner, connection);
                        break;
                    case 3:
                        updateStudent(scanner, connection);
                        break;
                    case 4:
                        deleteStudent(scanner, connection);
                        break;
                    case 5:
                        System.out.println("程序已退出，保存所有修改。");
                        return;
                    default:
                        System.out.println("输入的选项无效，请重新输入。");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    private static void showMenu() {
        System.out.println("======= 学生信息管理系统 =======");
        System.out.println("1. 添加学生信息");
        System.out.println("2. 检索学生信息");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 删除学生信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入操作编号 (1-5): ");
    }
    private static void addStudent(Scanner scanner, Connection connection) throws SQLException {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (isIdExists(id, connection)) {
            System.out.println("学号已存在，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        String sql = "INSERT INTO students (id, name, phone) VALUES (?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, id);
        preparedStatement.setString(2, name);
        preparedStatement.setString(3, phone);
        preparedStatement.executeUpdate();
        System.out.println("学生信息添加成功。");
    }
    private static boolean isIdExists(String id, Connection connection) throws SQLException {
        String sql = "SELECT COUNT(*) FROM students WHERE id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        resultSet.next();
        int count = resultSet.getInt(1);
        return count > 0;
    }
    private static void searchStudent(Scanner scanner, Connection connection) throws SQLException {
        System.out.println("请输入要检索的学号（输入QUIT退出检索）:");
        while (true) {
            String id = scanner.nextLine();
            if ("QUIT".equals(id)) {
                break;
            }
            String sql = "SELECT * FROM students WHERE id =?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Student student = new Student(resultSet.getString("id"), resultSet.getString("name"), resultSet.getString("phone"));
                System.out.println(student);}else{
                System.out.println("未找到该学生");
            }}}
    private static void updateStudent(Scanner scanner,Connection connection) throws SQLException{
        System.out.println("请输入要更新信息的学号（输入QUIT退出更新）：");
        while(true){
            String id= scanner.nextLine();
            if ("QUIT".equals(id)) {
                break;
            }
            if(!isIdExists(id,connection)){
                System.out.println("未找到该学生");
                continue;
            }
            System.out.println("请输入新的姓名：");
            String name=scanner.nextLine();
            System.out.print("请输入新的电话");
            String phone=scanner.nextLine();
            String sql="UPDATE students SET name=?,phone=?,WHERE id=?";
            PreparedStatement preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,phone);
            preparedStatement.setString(3,id);
            preparedStatement.executeUpdate();
            System.out.println("学生信息更新成功");
        }}
    private static  void deleteStudent(Scanner scanner,Connection connection) throws SQLException{
        System.out.println("请输入要删除的学号（输入QUIT退出更新）：");
        while(true){
            String id= scanner.nextLine();
            if ("QUIT".equals(id)) {
                break;
            }
            if(!isIdExists(id,connection)){
                System.out.println("未找到该学生");
                continue;
            }
            String sql="DELETE FROM students WHERE id=?";
            PreparedStatement preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            preparedStatement.executeUpdate();
            System.out.println("学生信息删除成功");
        }}
}